package terra.model.visual;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import terra.Config;
import terra.managers.LightManager;
import terra.managers.ResourceManager;
import terra.model.GameObject;
import terra.slick.GameState;

public class AnimationVisualizator extends AbstractVisualizator
{
	long time;
	short animationFrame = 0;
	
	@Override
	public void draw(Graphics g, GameObject object, float x, float y) {
		Image currentFrame = ((Animation) render).getImage(animationFrame);
		if (blend) {
			int x1 = GameState.getInstance().getTiledOffsetX()-4;
			int y1 = GameState.getInstance().getTiledOffsetY()-4;
			LightManager.getInstance().blend((int)(object.getX()/Config.tileSize-x1),(int)(object.getY()/Config.tileSize-y1),currentFrame);
		}
		else
			LightManager.eraseBlend(currentFrame);
			
		if (width == -1 ||  height == -1)
		{
			if (!object.isLeft())
				currentFrame.draw(x, y);
			else
				currentFrame.getFlippedCopy(true, false).draw(x, y);
		}
		else
		{
			if (!object.isLeft())
				currentFrame.draw(x, y,width,height);
			else
				currentFrame.getFlippedCopy(true, false).draw(x, y,width,height);
		}
	}

	@Override
	public void update(int delta)
	{
		time+=delta;
		if (time > ResourceManager.getAnimation(renderName).getDuration(animationFrame))
		{
			animationFrame+=1;
			time = 0;
		}
		if (animationFrame > ResourceManager.getAnimation(renderName).getFrameCount()-1)
			animationFrame = 0;
	}

}
